package com.mediacloud.app.newsmodule.fragment.works

import android.content.Intent
import androidx.recyclerview.widget.LinearLayoutManager
import com.mediacloud.app.model.fragment.BaseFragment
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.fragment.baoliao.api.SpiderCmsApi
import com.mediacloud.app.quanzi.model.BaseDataListBean
import com.mediacloud.app.user.model.UserInfo
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.android.synthetic.main.fragment_mysubmitlibrary.*
import okhttp3.MediaType
import okhttp3.RequestBody
import org.json.JSONObject
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

/**
 * 提交库
 */
class SubmitLibraryFragment : BaseFragment(), OnRefreshLoadMoreListener {

    var pageNumber = 1
    var pageSize = 20
    val adapter by lazy {
        SubmitLibraryAdapter()
    }

    override fun getLayoutResID(): Int {
        return R.layout.fragment_mysubmitlibrary
    }

    override fun initView() {

        loadingView = findViewById(R.id.mLoadingView)
        initStateView()
        emptyText?.setOnClickListener {
            pageNumber = 1
            getData()
        }
        showStateView(TYPE_LOADING, false)
        freshLayout.setOnRefreshLoadMoreListener(this)
        rvLayout.layoutManager = LinearLayoutManager(
            requireContext(),
            LinearLayoutManager.VERTICAL, false
        )
        rvLayout.adapter = adapter
        adapter.setOnItemClickListener { adapter, view, position ->
            val item = adapter.getItem(position) as ArticleBean
            val intent = Intent()
            intent.setClassName(
                requireContext(),
                "com.mediacloud.app.appfactory.activity.contribute.PreviewActivity"
            )
            intent.putExtra("articleId", item.articleId)
            startActivityForResult(intent,100)
        }
        getData()
    }

    private fun getData() {
        val jsonObject = JSONObject()
        jsonObject.put("pageNumber", pageNumber)
        jsonObject.put("pageSize", pageSize)
        jsonObject.put("addUser", UserInfo.getUserInfo(requireContext()).userid)
        jsonObject.put("status", 1)

        val body = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString())
        SpiderCmsApi.getSpiderCmsApi(requireContext())
            .getArticleList(body)
            .enqueue(object : Callback<BaseDataListBean<ArticleBean>> {
                override fun onResponse(
                    call: Call<BaseDataListBean<ArticleBean>>,
                    response: Response<BaseDataListBean<ArticleBean>>
                ) {
                    closeStateView()
                    freshLayout.finishRefresh()
                    if (response.isSuccessful) {
                        response.body()?.data?.let {
                            if (it.isHasNextPage) {
                                freshLayout.finishLoadMore()
                            } else {
                                freshLayout.finishLoadMoreWithNoMoreData()
                            }

                            it.pageRecords?.let { data ->
                                if (data.isEmpty()) {
                                    if (pageNumber == 1) {
                                        showStateView(TYPE_NO_CONTENT, false)
                                    }
                                } else {
                                    if (pageNumber == 1) {
                                        adapter.setNewData(data)
                                    } else {
                                        adapter.addData(data)
                                    }
                                }
                            }


                        }
                    }

                }

                override fun onFailure(call: Call<BaseDataListBean<ArticleBean>>, t: Throwable) {
                    showStateView(TYPE_NO_CONTENT, false)
                }

            })
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        pageNumber = 1
        getData()

    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        pageNumber++
        getData()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 100){
            pageNumber = 1
            getData()
        }
    }

}